﻿#from numpy import array
from numpy import *
from NewsMining.db.dbConnect import getDBConnection
from mynewsweb.model import *
from math import sqrt
from NewsMining.crawler.preprocessText import *
from sskFind import *
from cache import cached_kernel
from realSsk import callssk

#==== ==== ==== ====
# Settings
#==== ==== ==== ====
k = 5
decay = 0.5

#==== ==== ==== ====
# DATA
#==== ==== ==== ====
db = getDBConnection()
news = db.query(News).filter(News.rank != None).all()

title_unicode_trans = TitleUnicodeTranslate()

# ==== TEST ====
test = {0: u"Dusan Smitran",
        10: u"Dusan Smitran",
        1:  u"Dusanov Smitranovi",
        2:  u"Grega Kovacic",
        3:  u"Lekarni Ljubljana pol leta prodajali brez registracije, bodo na ministrstvu ukrepali oziroma bo to storila agencija za zdravila, je zatrdil minis1",
        4:  u"Lekarne Ljubljana so s polic umaknile sporna zdravila. (Foto: Anže Petkovšek) Kot se je izkazalo kasneje, je bilo sedem izdelkov, ki so jih v Lekarni Ljubljana prodajali kot ",
        5:  u"vana o vsem skupaj ne govori. (Foto: Borut Cvetko, Mediaspeed) Estradnica Ivana &Scaron;undov Hojan , ki živi v Zagrebu, se je v medijih pohvalila, da je postala zastopnica za popularne uggice. Direktor podjetja Logos Trend Miha Hrovat trdi, da Ivana pro"}

test_data = {}

for t in test:
    text = removeStopWords(simpleNormalize(test[t], title_unicode_trans))
    if len(text) > 5:
        test_data[t] = text

# ==== REAL ====
news = db.query(News).filter(News.rank != None).all()
news_data = {}

for n in news:
    text = removeStopWords(simpleNormalize(n.content, title_unicode_trans))
    if len(text) > 1000:
        news_data[n.news_id] = text

#sdfdsf
#==== ==== ==== ====
# CALL
#==== ==== ==== ====
kernel = cached_kernel(callssk)


##n1 = 249 #66, 249#
### ONE vs ALL smitran 2
##for n2 in news_data.keys()[:]:
##    if n1 != n2 and n2 in [84]: #142, 147, 392,
##        d = kernel(n1, n2, news_data, k, decay) / sqrt(kernel(n1, n1, news_data, k, decay) * kernel(n2, n2, news_data, k, decay))
##        print d
##        if d > 0:
##            r = KernelResults(n1, n2, d, 2, k, decay)
##            db.add(r)
##
##    db.commit()


### ALL vs ALL smitran2
##for n1 in news_data.keys()[:10]:
##    print n1
##    for n2 in news_data.keys()[:]:
##        if n1 != n2:
##            d = kernel(n1, n2, news_data) / sqrt(kernel(n1, n1, news_data) * kernel(n2, n2, news_data))
##            #print d
##            if d > 0:
##                r = KernelResults(n1, n2, d, 2)
##                db.add(r)
##
##    db.commit()

##profile.run("smitran_kernel2(66, 147, news_data)")
##print smitran_kernel2(66, 147, news_data)
##
##prag = 0.01
##for i in foo:
##    if foo[i][0] > prag and foo[i][1] > prag:
##        print "'%s'" % i, round(foo[i][0], 5), round(foo[i][1], 5), round(foo[i][0] * foo[i][1], 5)
